package com.javer.core;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 第三词大全缓存
 * @author z3y2
 */
public final class SensWordCache {
	private static Log logger = LogFactory.getLog(SensWordCache.class);
	private static final int Low = 1 << 0;
	private static final int Medium = 1 << 1;
	private static final int High = 1 << 2;
	private static Map<String ,String> lowSensWords;
	private static Map<String ,String> mediumSensWords;
	private static Map<String ,String> highSensWords;
	
	public static void initialize(String sensLevel) throws IOException {
		lowSensWords = new HashMap<String, String>();
		mediumSensWords = new HashMap<String, String>();
		highSensWords = new HashMap<String, String>();
		
		logger.info("敏感词配置参数为：" + sensLevel);
		
		int sensLevelInt = 0x000;
		
		try {
			sensLevelInt = Integer.valueOf(sensLevel, 2);
		}
		catch (NumberFormatException e) {
			logger.error("敏感词级别配置格式(如：0x011)错误", e);
		}
		
		if ((Low & sensLevelInt) == Low) {
			initMap(lowSensWords, "/sens_low.ini");
		}
		logger.info("低级敏感词数为：" + lowSensWords.size());
		
		if ((Medium & sensLevelInt) == Medium) {
			initMap(mediumSensWords, "/sens_medium.ini");
		}
		logger.info("中级敏感词数为：" + mediumSensWords.size());
		
		if ((High & sensLevelInt) == High) {
			initMap(highSensWords, "/sens_high.ini");
		}
		logger.info("高级敏感词数为：" + highSensWords.size());
	}
	
	public static int getLowSize() {
		return lowSensWords.size();
	}
	
	public static int getMediumSize() {
		return mediumSensWords.size();
	}
	
	public static int getHighSize() {
		return highSensWords.size();
	}
	
	private static void initMap(Map<String, String> map, String path) throws IOException {
		Properties prop = new Properties();
		prop.load(getReader(path));
		Iterator<Entry<Object, Object>> iter = prop.entrySet().iterator();
		
		for (Entry entry = iter.next(); iter.hasNext(); entry = iter.next()) {
			map.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
		}
	}
	
	private static InputStreamReader getReader(String path) throws UnsupportedEncodingException {
		return new InputStreamReader(SensWordCache.class.getResourceAsStream(path), "UTF-8");
	}
	
	public static Iterator<Entry<String, String>> iterateLow() {
		return lowSensWords.entrySet().iterator();
	}
	
	public static Iterator<Entry<String, String>> iterateMedium() {
		return mediumSensWords.entrySet().iterator();
	}
	
	public static Iterator<Entry<String, String>> iterateHigh() {
		return highSensWords.entrySet().iterator();
	}
	
	public static String getLow(String key) {
		return lowSensWords.get(key);
	}
	
	public static String getMedium(String key) {
		return mediumSensWords.get(key);
	}
	
	public static String getHigh(String key) {
		return highSensWords.get(key);
	}
	
}
